package com.yangrd.airship.common.utils;

import cn.dev33.satoken.stp.StpUtil;
import com.yangrd.airship.common.UserInfo;
import com.yangrd.airship.common.UserInfoService;
import com.yangrd.airship.common.spring.SpringContextHolder;

/**
 * description: PrincipalUtils
 *
 * @author: yangrd
 * @date: 2021/2/4
 * @version: 1.0
 */
public class PrincipalUtils {

    static UserInfoService userService = SpringContextHolder.getBean(UserInfoService.class);

    public static Integer getCurrentUserId() {
        return StpUtil.getLoginIdAsInt();
    }

    public static String getCurrentUsername() {
        return userService.loadUser(getCurrentUserId()).map(UserInfo::getUsername).orElseThrow(RuntimeException::new);
    }


}
